export const uniDay = {
	/**
	 * 倒计时
	 */
	countDown: function(endTime) {
		let startTime = new Date()
		if (endTime.indexOf('-') != -1) {
			endTime = endTime.replace(/\-/g, '/')
		}
		let downTime = new Date(endTime)
		let defTime = downTime.getTime() - startTime.getTime()
		let day = Math.floor(defTime / (1000 * 60 * 60 * 24))
		let hours = this.addZero(Math.floor((defTime / (1000 * 60 * 60)) % 24))
		let minutes = this.addZero(Math.floor((defTime / (1000 * 60)) % 60))
		let seconds = this.addZero(Math.floor((defTime / 1000) % 60))

		return {
			day,
			hours,
			minutes,
			seconds
		}
	},

	addZero: function(num) {
		if (num < 10) return `0${num}`
		return num
	},

	chageDataType: function(str, type) {
		if (type == '/') {
			return str.replace(/\-/g, '/')
		}
		if (type == '-') {
			return str.replace(/\//g, '-')
		}
		if (type == 'hh:mm:ss') {
			return wxCommon.split(str, ' ')[1]
			// return new Date(str).toLocaleTimeString('zh',{hour12:false})
		}
		if (type == 'yy-mm-dd') {
			return wxCommon.split(str, ' ')[0]
		}
		if (type == 'chain-date') {
			let date = new Date(str)
			let yaer = date.getFullYear()
			let month = date.getMonth() + 1
			let day = date.getDate()
			let hours = date.getHours()
			let minutes = date.getMinutes()
			let seconds = date.getSeconds()
			return `${yaer}年${month}月${day}日 ${hours}时${minutes}分${seconds}秒`
		}
		if (type == 'week') {
			let index = new Date(str).getDay()
			let weekArr = ['日', '一', '二', '三', '四', '五', '六']
			return `周${weekArr[index]}`
		}

		if (type == 'actral') {
			let myTimes = new Date(str)
			let yaer = myTimes.getFullYear()
			let month = myTimes.getMonth() + 1
			let day = myTimes.getDate()
			let hours = myTimes.getHours()
			hours = this.addZero(hours)
			let minutes = myTimes.getMinutes()
			minutes = this.addZero(minutes)
			return `${yaer}年${month}月${day}日 ${hours}:${minutes}`
		}
	},


	/**
	 * 获取日期 如：2018/08/12 23:00:00 如time?为false则返回当前日期时间
	 * @param {string|number} time 日期 时间戳 等
	 * @param {string} type 返回的类型
	 */
	getTime: function(time, type) {
		let myTimes = time ? new Date(time) : new Date()
		let yaer = myTimes.getFullYear()
		let month = myTimes.getMonth() + 1
		if (month < 10) month = '0' + month
		let day = myTimes.getDate()
		if (day < 10) day = '0' + day
		let hours = myTimes.getHours()
		let minutes = myTimes.getMinutes()
		let seconds = myTimes.getSeconds()
		let entTime = `${yaer}/${month}`
		return type ? this.chageDataType(entTime, type) : myTimes
	},

	formatTime: function(num) {
		let endTime = new Date(num).getTime()
		let startTime = new Date().getTime()

		let defTime = startTime - endTime
		let day = Math.floor(defTime / (1000 * 60 * 60 * 24))
		let hours = Math.floor((defTime / (1000 * 60 * 60)) % 24)
		let minutes = Math.floor((defTime / (1000 * 60)) % 60)
		let seconds = Math.floor((defTime / 1000) % 60)

		if (day > 0 && day < 2) {
			return `${day}天前`
		} else if (day == 0) {
			if (hours > 0) {
				return `${hours}小时前`
			} else if (hours == 0) {
				if (minutes > 0) {
					return `${minutes}分钟前`
				} else if (minutes == 0) {
					return '刚刚'
				}
			}
		} else {
			let currentTime = new Date(num)
			let currentyaers = currentTime.getFullYear()
			let currentmonths = currentTime.getMonth() + 1
			let currentdays = currentTime.getDate()
			let currenthours = currentTime.getHours()
			currenthours = this.addZero(currenthours)
			let currentminutes = currentTime.getMinutes()
			currentminutes = this.addZero(currentminutes)
			return `${currentyaers}年${currentmonths}月${currentdays}日 ${currenthours}:${currentminutes}`
		}
	}


}